# RISC-V 參考資料下載腳本
# 根據指導手冊下載所需的參考項目

Write-Host "開始下載 RISC-V 參考資料..." -ForegroundColor Green

# 創建資料夾結構
$folders = @("xv6-riscv", "docs", "specs", "manuals")
foreach ($folder in $folders) {
    if (!(Test-Path $folder)) {
        New-Item -ItemType Directory -Path $folder -Force
        Write-Host "創建資料夾: $folder" -ForegroundColor Yellow
    }
}

# 1. 下載 xv6-riscv 源碼
Write-Host "正在下載 xv6-riscv 源碼..." -ForegroundColor Cyan
try {
    if (!(Test-Path "xv6-riscv")) {
        git clone https://github.com/mit-pdos/xv6-riscv.git
        Write-Host "xv6-riscv 下載完成" -ForegroundColor Green
    } else {
        Write-Host "xv6-riscv 已存在，跳過下載" -ForegroundColor Yellow
    }
} catch {
    Write-Host "xv6-riscv 下載失敗: $($_.Exception.Message)" -ForegroundColor Red
}

# 2. 下載 RISC-V 規範文檔
Write-Host "正在下載 RISC-V 規範文檔..." -ForegroundColor Cyan
$riscvSpecs = @(
    @{
        Name = "RISC-V Privileged Specification"
        Url = "https://github.com/riscv/riscv-isa-manual/releases/latest/download/riscv-privileged.pdf"
        File = "specs/riscv-privileged.pdf"
    },
    @{
        Name = "RISC-V Unprivileged Specification"
        Url = "https://github.com/riscv/riscv-isa-manual/releases/latest/download/riscv-unprivileged.pdf"
        File = "specs/riscv-unprivileged.pdf"
    }
)

foreach ($spec in $riscvSpecs) {
    try {
        if (!(Test-Path $spec.File)) {
            Write-Host "下載 $($spec.Name)..." -ForegroundColor Yellow
            Invoke-WebRequest -Uri $spec.Url -OutFile $spec.File -UseBasicParsing
            Write-Host "$($spec.Name) 下載完成" -ForegroundColor Green
        } else {
            Write-Host "$($spec.Name) 已存在，跳過下載" -ForegroundColor Yellow
        }
    } catch {
        Write-Host "$($spec.Name) 下載失敗: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# 3. 下載 xv6 手冊
Write-Host "正在下載 xv6 手冊..." -ForegroundColor Cyan
$xv6Manual = @{
    Name = "xv6 Book"
    Url = "https://pdos.csail.mit.edu/6.828/2025/xv6/book-riscv-rev5.pdf"
    File = "manuals/xv6-book-riscv-rev5.pdf"
}

try {
    if (!(Test-Path $xv6Manual.File)) {
        Write-Host "下載 $($xv6Manual.Name)..." -ForegroundColor Yellow
        Invoke-WebRequest -Uri $xv6Manual.Url -OutFile $xv6Manual.File -UseBasicParsing
        Write-Host "$($xv6Manual.Name) 下載完成" -ForegroundColor Green
    } else {
        Write-Host "$($xv6Manual.Name) 已存在，跳過下載" -ForegroundColor Yellow
    }
} catch {
    Write-Host "$($xv6Manual.Name) 下載失敗: $($_.Exception.Message)" -ForegroundColor Red
}

# 4. 下載其他有用的參考資料
Write-Host "正在下載其他參考資料..." -ForegroundColor Cyan
$additionalDocs = @(
    @{
        Name = "RISC-V Calling Convention"
        Url = "https://riscv.org/wp-content/uploads/2015/01/riscv-calling.pdf"
        File = "docs/riscv-calling-convention.pdf"
    },
    @{
        Name = "SBI Specification"
        Url = "https://github.com/riscv-non-isa/riscv-sbi-doc/releases/latest/download/riscv-sbi.pdf"
        File = "docs/riscv-sbi.pdf"
    }
)

foreach ($doc in $additionalDocs) {
    try {
        if (!(Test-Path $doc.File)) {
            Write-Host "下載 $($doc.Name)..." -ForegroundColor Yellow
            Invoke-WebRequest -Uri $doc.Url -OutFile $doc.File -UseBasicParsing
            Write-Host "$($doc.Name) 下載完成" -ForegroundColor Green
        } else {
            Write-Host "$($doc.Name) 已存在，跳過下載" -ForegroundColor Yellow
        }
    } catch {
        Write-Host "$($doc.Name) 下載失敗: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# 5. 創建 README 文件
$readmeContent = @"
# RISC-V 參考資料

本資料夾包含從零構建操作系統指導手冊中提到的所有參考項目和文檔。

## 資料夾結構

- `xv6-riscv/` - xv6-riscv 源碼，主要的參考實現
- `specs/` - RISC-V 規範文檔
  - `riscv-privileged.pdf` - RISC-V 特權級規範
  - `riscv-unprivileged.pdf` - RISC-V 非特權級規範
- `manuals/` - 操作系統相關手冊
  - `xv6-book-riscv-rev5.pdf` - xv6 手冊
- `docs/` - 其他參考文檔
  - `riscv-calling-convention.pdf` - RISC-V 調用約定
  - `riscv-sbi.pdf` - SBI 規範

## 使用說明

1. **xv6-riscv 源碼**：這是主要的參考實現，包含完整的操作系統代碼
2. **RISC-V 規範**：理解 RISC-V 架構的官方文檔
3. **xv6 手冊**：詳細解釋 xv6 操作系統的設計和實現

## 下載時間

下載完成時間：$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")
"@

$readmeContent | Out-File -FilePath "README.md" -Encoding UTF8

Write-Host "`n所有參考資料下載完成！" -ForegroundColor Green
Write-Host "請查看 README.md 了解詳細說明" -ForegroundColor Cyan
